require 'dotenv'
require 'fileutils'
require 'pyro/compiler'
require 'pyro/minifier'
require 'sinatra/base'

# Namespaces Pyro::Helpers.
module Pyro
  # Public: Interface helpers for Pyro::App.
  #
  # * Includes Pyro::Compiler,
  #   Pyro::Linker (via Pyro::Compiler),
  #   and Pyro::Minifier.
  #
  # Example
  #
  #   <%= script src: 'app/config/application.coffee' %>
  module Helpers
    include Compiler
    include Minifier

    # Public: Renders index.erb.
    #
    # Example
    #
    #   render()
    #
    # Renders app/config/index.erb into public/index.html
    def  render
      Pyro::App.set :links, []
      Pyro::App.set :timestamp, Time.now.strftime('%Y%m%d%H%M%S')

      FileUtils.rm_rf settings.public_folder
      FileUtils.mkdir settings.public_folder

      index = File.read("#{settings.root}/app/config/index.erb")
      rendered = ERB.new(index, nil, '-').result(binding)
      IO.write "#{settings.public_folder}/index.html", rendered, mode: 'a'
    end

    # Public: Parses, compiles, and writes .js and .coffee into /public.
    #
    # src      - String name of a file to convert.
    # prod_src - Overrides src in the production environment.
    # dir      - String path of a directory to convert.
    # prod_dir - Overrides dir in the production environment.
    # target   - File to write converted content into.
    #
    # Example
    #
    #   <%= script dir: 'pods' -%>
    #
    #  Returns a <script> tag linking to the new .js file.
    def script(*args)
      compile args.first, 'js,coffee'
    end

    # Public: Parses, compiles, and writes .css and .scss into /public.
    #
    # src      - String name of a file to convert.
    # prod_src - Overrides src in the production environment.
    # dir      - String path of a directory to convert.
    # prod_dir - Overrides dir in the production environment.
    # target   - File to write converted content into.
    #
    # Example
    #
    #   <%= stylesheet dir: 'resources' -%>
    #
    #  Returns a <link> tag linking to the new .js file.
    def stylesheet(*args)
      compile args.first, 'css,scss'
    end

    # Public: Parses, compiles, and writes .hbs and .handlebars into /public.
    #
    # src      - String name of a file to convert.
    # prod_src - Overrides src in the production environment.
    # dir      - String path of a directory to convert.
    # prod_dir - Overrides dir in the production environment.
    # name     - Name of converted template.
    # target   - File to write converted content into.
    #
    # Example
    #
    #   <%= template dir: 'pods' -%>
    #
    #  Returns a <script> tag linking to the new .js file.
    def template(*args)
      compile args.first, 'hbs,handlebars'
    end

    # Public: Alias for script() that runs only in the test environment.
    def test_script(*args)
      script(args.first) if settings.environment == :test
    end

    # Public: Alias for stylesheet() that runs only in the test environment.
    def test_stylesheet(*args)
      stylesheet(args.first) if settings.environment == :test
    end

    # Public: Alias for template() that runs only in the test environment.
    def test_template(*args)
      template(args.first) if settings.environment == :test
    end
  end
end